"""
    @Author  ：思念 
    @File    ：10.使用bs4提取搜狗微信的信息.py
    @Date    ：2024/12/7 14:25 
"""
from bs4 import BeautifulSoup
import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0'
}

url = 'https://weixin.sogou.com/weixin?_sug_type_=1&type=2&query=python'

res = requests.get(url, headers=headers)

soup = BeautifulSoup(res.text, 'lxml')

# 提取h3标签下所有文本内容
elements = soup.find("ul", class_="news-list").find_all("h3")
for element in elements:
    print("文本信息：", element.get_text().strip())
    print("详情链接：", 'https://weixin.sogou.com/' + element.find('a').get('href'))
# 提取所有图片链接
elements = soup.find("ul", class_="news-list").find_all("img")
for element in elements:
    print("图片链接：", element.get('src').replace('//', ""))


